package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.QuestionModel;
import service.ImportUrlThread;

/**
 * Servlet implementation class ImportServlet
 */
@WebServlet("/ImportServlet")
public class ImportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("url") != null) {
			String url = request.getParameter("url").toString();
			if (!url.equals("")) {
				QuestionModel questionModel = new QuestionModel();
				if (!questionModel.isGotten(url)) {
					int randomNum = 0 + (int) (Math.random() * 7);
					String socket = "184.107.59.4" + randomNum;
					ImportUrlThread importThread = new ImportUrlThread(url,
							socket);
					importThread.start();					
				} else {
					System.out.println("Question had already got !");
				}				
				request.getRequestDispatcher("/import.jsp").forward(request,
						response);
			}
		}
	}

}
